# single auto modeling interface for different spatial point process model
#   auto here means it will do covariable selection automatically
#
# model: names of model; current availible model names are 
#        "Poisson"(default), "LGCP", "Thomas"
#
# Author: Guochun Shen
# Data:   2011-12-18
# Project:spatial statistic
# Email:  shenguochun@gmail.com
###############################################################################

auto_modeling=function(data.ppp,model="HetePoisson",covariates = NULL,...){
	#since there is no implemented marked point process
	data.ppp=unmark(data.ppp)
	if(!is.null(covariates)){
		formu0=auto_formu(names(covariates))
		#step1 fit a poisson model first
		pre_fitted=ppm(data.ppp,formu0,covariates=covariates)
		if(pre_fitted$internal$glmfit$converged){
			pre_fitted=step(pre_fitted,trace=0)
			formu0=pre_fitted$trend
		}else{
		#if not convergent, just fitted by Poisson process
			pre_fitted=ppm(data.ppp)
			formu0=auto_formu(numeric())
			covariates=NULL
		}
	}else{
		formu0=auto_formu(numeric())
	}
	
	if(model=="LGCP" || model=="Thomas"){
		fitted=kppm(data.ppp,formu0,clusters=model,covariates)
	}else if(model=="HetePoisson"){
		fitted=pre_fitted
	}
		
	
	return(fitted)
}

